import { In, Repository } from 'typeorm';
import { Role } from './entities/role.entity';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { UserService } from '../user/user.service';

@Injectable()
export class RoleService {
  constructor(
    @InjectRepository(Role) private roleRepository: Repository<Role>,
    private readonly userService: UserService,
  ) {}
  async getAllRoles(): Promise<Role[]> {
    return this.roleRepository.find();
  }
  async findRolesByUsername(username: string) {
    const user = await this.userService.find(username);
    return await this.roleRepository.find({
      where: {
        id: In(user.roles.map((it) => it.id)),
      },
    });
  }
}
